package backtracking.nowcoder.medium;

import java.util.Scanner;

/**
 * @author bruin_du
 * @description 红与黑
 * @date 2022/6/29 16:17
 **/
public class RedAndBlack {
    static int count = 0;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()) {
            int row = in.nextInt();
            int col = in.nextInt();
            in.nextLine();
            int indexRow = 0,indexCol = 0;
            char[][] arr = new char[row][col];
            for (int i = 0; i < row; i++) {
                String tmp = in.nextLine();
                for (int j = 0; j < col; j++) {
                    arr[i][j] = tmp.charAt(j);
                    if(arr[i][j] == '@') {
                        indexRow = i;
                        indexCol = j;
                        arr[i][j] = '.';
                    }
                }
            }
            countBlackBlocks(arr,row,col,indexRow,indexCol);
            System.out.println(count);
            count = 0;
        }
    }

    private static void countBlackBlocks(char[][] arr, int row, int col, int indexRow, int indexCol) {
        if(indexRow < 0 || indexRow >= row || indexCol < 0 || indexCol >= col || arr[indexRow][indexCol] != '.') {
            return;
        }
        arr[indexRow][indexCol] = '#';
        count++;
        countBlackBlocks(arr, row, col, indexRow + 1, indexCol);
        countBlackBlocks(arr, row, col, indexRow - 1, indexCol);
        countBlackBlocks(arr, row, col, indexRow, indexCol + 1);
        countBlackBlocks(arr, row, col, indexRow, indexCol - 1);
    }
}
